//
//  XML_Loader.m
//  test_xml
//
//  Created by bean on 13/1/4.
//
//

#import "XML_Loader.h"
#import "DataSheet.h"
#import "DDXML.h"
@implementation XML_Loader
+ (NSDictionary*)LoaderXML_WithName:(NSString*)name
{
    NSMutableDictionary* sheetDict = [[NSMutableDictionary alloc] init];
    NSString* filePath = [[NSBundle mainBundle] pathForResource:name ofType:@"xml"];
    NSString* xmlString = [NSString  stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    
    DDXMLDocument* xmlDoc = [[DDXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];
    NSArray* rootNode = [xmlDoc nodesForXPath:@"database/sheet" error:nil];
    for (NSInteger i = 0; i < [rootNode count]; i++)
    {
        DataSheet* dataSheet = [[DataSheet alloc] init];
        [self parseSheetWithXmlNode:[rootNode objectAtIndex:i] Sheet:dataSheet];
        NSLog(@"data sheet name = %@",[dataSheet getSheetName]);
        
        for (int j = 0; j < [dataSheet getColumnCount]; j++)
        {
            for (int k = 0; k < [dataSheet getRowCount]; k++)
            {
                NSLog(@"name = %@ , data = %@",[dataSheet getColumnWithIndex:j], [dataSheet getRowDataByColumnIndexWithRowIndex:k columnIndex:j]);
            }
        }
        
        [sheetDict setValue:dataSheet forKey:[dataSheet getSheetName]];
        
        [dataSheet release];
    }
    
    [xmlDoc release];
    
    return [sheetDict autorelease];
}
+ (DataSheet*)parseSheetWithXmlNode:(DDXMLNode*)sheetNode Sheet:(DataSheet*)sheetData
{
    DDXMLElement* element = (DDXMLElement*)sheetNode;
    
    [sheetData setSheetName:[[element attributeForName:@"name"] stringValue]];
    
    for (NSInteger i = 0; i < [sheetNode childCount]; i++)
    {
        DDXMLNode* node = [sheetNode childAtIndex:i];
        
        if ([[node localName] isEqualToString:@"columns"])
        {
            sheetData = [self parseSheetColumnWithSheetNode:node sheet:sheetData];
        }
        
        if ([[node localName] isEqualToString:@"data"])
        {
            sheetData = [self parseSheetDataWithSheetNode:node sheet:sheetData];
        }
    }
    
    return sheetData;
}

+ (DataSheet*)parseSheetColumnWithSheetNode: (DDXMLNode*)sheetNode sheet:(DataSheet*)sheetData
{
    for (NSInteger i = 0; i < [sheetNode childCount]; i++)
    {
        DDXMLElement* element = (DDXMLElement*)[sheetNode childAtIndex:i];
        
        [sheetData addColumnWithColumnName:[[element attributeForName:@"name"] stringValue]];
    }
    
    return sheetData;
}

+ (DataSheet*)parseSheetDataWithSheetNode:(DDXMLNode*)sheetNode sheet:(DataSheet*)sheetData
{
    NSInteger rowIndex = 0;
    
    for (NSInteger i = 0; i < [sheetNode childCount]; i++)
    {
        DDXMLNode* rowNode = [sheetNode childAtIndex:i];
        
        rowIndex = [sheetData createRow];
        
        for (NSInteger j = 0; j < [rowNode childCount]; j++)
        {
            NSString* rowData = [[rowNode childAtIndex:j] stringValue];
            [sheetData addRowDataWithRowIndex:rowIndex rowData:rowData];
        }
    }
    return sheetData;
}
@end
